# Analysis of the QAP results 
rm(list=ls())

# Function to load QAP Results and Extract Coefficients, T-Statistics, and Sampling Distribution
load_qap_results <- function(filenames) {
  QAP_result <- readRDS(filenames[1])
  coeffs <- QAP_result$coefficients
  tstats <- QAP_result$tstat
  dist_list <- list(QAP_result$dist)
  
  for (i in 2:length(filenames)) {
    qap_res <- readRDS(filenames[i])
    dist_list[[i]] <- qap_res$dist
    
    # Check coefficient and tstat equality
    coeff_check <- mapply(function(x, y) {isTRUE(all.equal(x, y))}, coeffs, qap_res$coefficients)
    tstat_check <- mapply(function(x, y) {isTRUE(all.equal(x, y))}, tstats, qap_res$tstat)
    print(table(coeff_check))
    print(table(tstat_check))
    
    rm(qap_res)
  }
  
  dist <- do.call(rbind, dist_list)
  return(list(coeffs = coeffs, tstats = tstats, dist = dist))
}


# Function to Compute P-Values and Create Results DataFrame
compute_pvalues_and_create_results <- function(coeffs, tstats, dist, covariate_names) {
  num_coeffs <- length(coeffs)
  pValues <- rep(NA, num_coeffs)
  
  for (i in 1:num_coeffs) {
    pValues[i] <- mean(abs(dist[,i]) >= abs(tstats[i]))
  }
  
  Var.names <- covariate_names  # List of variable names
  
  df.results <- data.frame(
    Variable = Var.names,
    Coefficient = coeffs,
    PValue = pValues
  )
  
  return(df.results)
}


#---------------------------------------------------------------------------------#
##########################  QAP with Same State interaction #######################

# Assign variable names
Var.names <- c("Intercept",
               "State Similarity",
               "Party Similarity",
               "Chamber Similarity",
               "Gender Similarity",
               "Race Similarity",
               "Difference in Legislatures Profeshionalism",
               "Dem Sender Effect",
               "Rep Sender Effect",
               "House Sender Effect",
               "Female Sender Effect",
               "Profesh Sender Effect",
               "Black Sender Effect",
               "Latino Sender Effect",
               "Asian Sender Effect",
               "Mena Sender Effect",
               "Multi Sender Effect",
               "Native Sender Effect",
               "Democrat Receiver Effect",
               "Republican Receiver Effect",
               "House Receiver Effect",
               "Female Receiver Effect",
               "Profesh Receiver Effect",
               "Black Receiver Effect",
               "Latino Receiver Effect",
               "Asian Receiver Effect",
               "Mena Receiver Effect",
               "Multi Receiver Effect",
               "Native Receiver Effect",
               "Same Party * Same State",
               "Same Chamber * Same State",
               "Same Gender * Same State",
               "Same Race * Same State",
               "Contiguous States")
#----------------------------------------------------------------------------------

# # Follower network results with different state interactions 
filenames <- Sys.glob("QAP_followers_SameState_analysis_scripts/output/qap_res_followers_out*")
len <- length(filenames)
qap_data <- load_qap_results(filenames)
results <- compute_pvalues_and_create_results(qap_data$coeffs, 
                                              qap_data$tstats, 
                                              qap_data$dist, 
                                              covariate_names=Var.names)

# Save data 
saveRDS(df.results, file="QAP_outputs/QAP_followers_SameState_output.Rds")
# Create latex table 
stargazer::stargazer(df.results, summary = F, type = "latex", 
                     out = "QAP_outputs/tables/qap_followers_SameState_result.tex")


# # Mention network results with different state interactions 
filenames <- Sys.glob("QAP_mentions_SameState_analysis_scripts/output/qap_res_mentions_out*")
len <- length(filenames)
qap_data <- load_qap_results(filenames)
results <- compute_pvalues_and_create_results(qap_data$coeffs, 
                                              qap_data$tstats, 
                                              qap_data$dist, 
                                              covariate_names=Var.names)
# Save data 
saveRDS(df.results, file="QAP_outputs/QAP_mentions_SameState_output.Rds")
# Create latex table 
stargazer::stargazer(df.results, summary = F, type = "latex", 
                     out = "QAP_outputs/tables/qap_mentions_SameState_result.tex")


# # Retweet network results with different state interactions # # 
filenames <- Sys.glob("QAP_retweets_SameState_analysis_scripts/output/qap_res_retweets_out*")
len <- length(filenames)
qap_data <- load_qap_results(filenames)
results <- compute_pvalues_and_create_results(qap_data$coeffs, 
                                              qap_data$tstats, 
                                              qap_data$dist, 
                                              covariate_names=Var.names)
# Save data 
saveRDS(df.results, file="QAP_outputs/QAP_retweets_SameState_output.Rds")
# Create latex table 
stargazer::stargazer(df.results, summary = F, type = "latex", 
                     out = "QAP_outputs/tables/qap_retweets_SameState_result.tex")


#---------------------------------------------------------------------------------#
########################## QAP with Different State interaction####################

# Assign variable names
Var.names <- c("Intercept",
               "State Similarity",
               "Party Similarity",
               "Chamber Similarity",
               "Gender Similarity",
               "Race Similarity",
               "Difference in Legislatures Profeshionalism",
               "Dem Sender Effect",
               "Rep Sender Effect",
               "House Sender Effect",
               "Female Sender Effect",
               "Profesh Sender Effect",
               "Black Sender Effect",
               "Latino Sender Effect",
               "Asian Sender Effect",
               "Mena Sender Effect",
               "Multi Sender Effect",
               "Native Sender Effect",
               "Democrat Receiver Effect",
               "Republican Receiver Effect",
               "House Receiver Effect",
               "Female Receiver Effect",
               "Profesh Receiver Effect",
               "Black Receiver Effect",
               "Latino Receiver Effect",
               "Asian Receiver Effect",
               "Mena Receiver Effect",
               "Multi Receiver Effect",
               "Native Receiver Effect",
               "Same Party * Diff State",
               "Same Chamber * Diff State",
               "Same Gender * Diff State",
               "Same Race * Diff State",
               "Contiguous States")
#----------------------------------------------------------------------------------
# # Follower network results with different state interactions 
filenames <- Sys.glob("QAP_followers_DiffState_analysis_scripts/output/qap_res_followers_out*")
len <- length(filenames)
qap_data <- load_qap_results(filenames)
results <- compute_pvalues_and_create_results(qap_data$coeffs, 
                                              qap_data$tstats, 
                                              qap_data$dist, 
                                              covariate_names=Var.names)

# Save data 
saveRDS(df.results, file="QAP_outputs_July2023/QAP_followers_DiffState_output.Rds")
# Create latex table 
stargazer::stargazer(df.results, summary = F, type = "latex", 
                     out = "QAP_outputs_July2023/tables/qap_followers_DiffState_result.tex")


# # Mention network results with different state interactions 
filenames <- Sys.glob("QAP_mentions_DiffState_analysis_scripts/output/qap_res_mentions_out*")
len <- length(filenames)
qap_data <- load_qap_results(filenames)
results <- compute_pvalues_and_create_results(qap_data$coeffs, 
                                              qap_data$tstats, 
                                              qap_data$dist, 
                                              covariate_names=Var.names)

# Save data 
saveRDS(df.results, file="QAP_outputs/QAP_mentions_DiffState_output.Rds")
# Create latex table 
stargazer::stargazer(df.results, summary = F, type = "latex", 
                     out = "QAP_outputs/tables/qap_mentions_DiffState_result.tex")


# # Retweet network results with different state interactions 
filenames <- Sys.glob("QAP_retweets_DiffState_analysis_scripts/output/qap_res_retweets_out*")
len <- length(filenames)
qap_data <- load_qap_results(filenames)
results <- compute_pvalues_and_create_results(qap_data$coeffs, 
                                              qap_data$tstats, 
                                              qap_data$dist, 
                                              covariate_names=Var.names)

# Save data 
saveRDS(df.results, file="QAP_outputs/QAP_retweets_DiffState_output.Rds")
# Create latex table 
stargazer::stargazer(df.results, summary = F, type = "latex", 
                     out = "QAP_outputs/tables/qap_retweets_DiffState_result.tex")
